﻿using System;
using System.IO;
//using Net.Common;
namespace Net.BLL
{
    /// <summary>
    /// 生成静态页相关方法
    /// </summary>
    /// Author: liufeng
    /// Created: 2012.7.25
    /// Email：liufeng2003@163.com
    public class StaticPage
    {
        //private static CYPLog.TextLogger m_objLog = CYPLog.TextLogManager.Create(typeof(StaticPage));

        #region 生成静态页
        /// <summary>
        /// 生成静态页
        /// </summary>
        /// <param name="paras">参数字符串</param>
        /// <param name="urlAspx">要生成的页面</param>
        /// <param name="saveadd">保存的文件夹路径</param>
        /// <param name="sCode">sCode文件名称前缀</param>
        /// <param name="encodingCode">编码</param>
        public static int CreateHtmlPage(string paras, string urlAspx, string saveadd, string sCode, string encodingCode)
        {
            System.IO.StreamReader sr = null;
            StreamWriter sw = null;
            //FileStream fs = null;
            string newfilename = string.Empty;
            try
            {
                string fileName = sCode + ".html";
                string tagFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/") + saveadd;
                if (!Directory.Exists(tagFolder))
                {
                    Directory.CreateDirectory(tagFolder);
                }
                newfilename = tagFolder + "/" + fileName;

                string urlAll = urlAspx + paras;
                System.Net.WebRequest httpWebRequest = System.Net.WebRequest.Create(urlAll);
                System.Net.WebResponse httpWebResponse = httpWebRequest.GetResponse();
                sr = new StreamReader(httpWebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding(encodingCode));

                //if (File.Exists(newfilename))
                //{
                //    File.Delete(newfilename);
                //}
                string txthtm = sr.ReadToEnd();
                sr.Close();
                //创建文件信息对象
                //fs = new FileStream(newfilename, FileMode.Create, FileAccess.Write);
                //FileInfo finfo = new FileInfo(newfilename);
                //根据上面创建的文件流创建写数据流
                //sw = new StreamWriter(fs,false,System.Text.Encoding.GetEncoding(EncodingCode));
                sw = new StreamWriter(newfilename, false, System.Text.Encoding.GetEncoding(encodingCode));
                //把新的内容写到创建的HTML页面中
                sw.WriteLine(txthtm);
                sw.Flush();
                sw.Close();
                return 1;
            }
            catch (Exception ex)
            {
                //m_objLog.Error("生成检测报告出错：文件名：" + newfilename + "具体错误：" + ex);
                if (sw != null) sw.Close();
                //fs.Close();
                if (File.Exists(newfilename))
                {
                    File.Delete(newfilename);
                }
                return -1;
            }
        }
        #endregion

        #region 生成静态页
        /// <summary>
        /// 生成静态页
        /// </summary>
        /// <param name="paras">参数字符串</param>
        /// <param name="urlAspx">要生成的页面</param>
        /// <param name="saveadd">保存的文件夹路径</param>
        /// <param name="sAucId">sAucId 竞拍ID</param>
        /// <param name="encodingCode">编码</param>
        ///
        public static int CreateHtmlPage(string paras, string urlAspx, string saveadd, int sAucId, string encodingCode)
        {
            System.IO.StreamReader sr = null;
            StreamWriter sw = null;
            //FileStream fs = null;
            string newfilename = string.Empty;
            try
            {
                //ReportFun car = DataFactory.CreateFunInstance<ReportFun>("ReportFun");
                string sCode = string.Empty;
                string sCarId = string.Empty;
                //sCode = car.GetTradeCodeFromAuctionId(sAucId.ToString(), ref sCarId);
                sCode = "123456789";
                string fileName = sCode + ".html";
                string tagFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/") + saveadd;
                if (!Directory.Exists(tagFolder))
                {
                    Directory.CreateDirectory(tagFolder);
                }
                newfilename = tagFolder + "/" + fileName;


                string urlAll = urlAspx + paras;
                System.Net.WebRequest httpWebRequest = System.Net.WebRequest.Create(urlAll);
                System.Net.WebResponse httpWebResponse = httpWebRequest.GetResponse();
                sr = new System.IO.StreamReader(httpWebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding(encodingCode));

                //if (File.Exists(newfilename))
                //{
                //    File.Delete(newfilename);
                //}
                string txthtm = sr.ReadToEnd();
                sr.Close();
                //fs = new FileStream(newfilename, FileMode.Create, FileAccess.Write);
                //FileInfo finfo = new FileInfo(newfilename);
                ////根据上面创建的文件流创建写数据流
                //sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(EncodingCode));
                //sw.WriteLine(txthtm);
                sw = new StreamWriter(newfilename, false, System.Text.Encoding.GetEncoding(encodingCode));
                //把新的内容写到创建的HTML页面中
                sw.WriteLine(txthtm);
                sw.Flush();
                sw.Close();
                return 1;
            }
            catch (Exception ex)
            {
                //m_objLog.Error("生成检测报告出错：文件名：" + newfilename + "具体错误：" + ex);
                if (sw != null) sw.Close();
                //fs.Close();
                if (File.Exists(newfilename))
                {
                    File.Delete(newfilename);
                }
                return -1;
            }
        }
        #endregion
    }
}
